package my.mark.mybaibaoxiang.platformBase.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 金额、
 * @Author wxming
 * @Date 2014年11月1日 下午4:30:42 
 */
public class MathUtil {

    /**
     * 除法精确10位小数且四舍五入
     * 
     * @param dividend
     * @param divisor
     * @return
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) {
        return dividend.divide(divisor, 10, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 判断是否为数字，包括小数，负数
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]*.?[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 小写金额转大写
     * 处理的最大数字达千万亿位 精确到分
     * @return
     */
    public static String digitUppercase(String num) throws Exception {
        String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        /**
         *      仟        佰        拾         ' '
         ' '    $4        $3        $2         $1
         万     $8        $7        $6         $5
         亿     $12       $11       $10        $9
         */
        String unit1[] = { "", "拾", "佰", "仟" };//把钱数分成段,每四个一段,实际上得到的是一个二维数组
        String unit2[] = { "元", "万", "亿", "万亿" }; //把钱数分成段,每四个一段,实际上得到的是一个二维数组
        BigDecimal bigDecimal = new BigDecimal(num);
        bigDecimal = bigDecimal.multiply(new BigDecimal(100));
        //        Double bigDecimal = new Double(name*100);     存在精度问题 eg：145296.8
        String strVal = String.valueOf(bigDecimal.toBigInteger());
        String head = strVal.substring(0, strVal.length() - 2); //整数部分
        String end = strVal.substring(strVal.length() - 2); //小数部分
        String endMoney = "";
        String headMoney = "";
        if ("00".equals(end)) {
            endMoney = "整";
        } else {
            if (!end.substring(0, 1).equals("0")) {
                endMoney += digit[Integer.valueOf(end.substring(0, 1))] + "角";
            } else if (end.substring(0, 1).equals("0") && !end.substring(1, 2).equals("0")) {
                endMoney += "零";
            }
            if (!end.substring(1, 2).equals("0")) {
                endMoney += digit[Integer.valueOf(end.substring(1, 2))] + "分";
            }
        }
        char[] chars = head.toCharArray();
        Map<String, Boolean> map = new HashMap<String, Boolean>();//段位置是否已出现zero
        boolean zeroKeepFlag = false;//0连续出现标志
        int vidxtemp = 0;
        for (int i = 0; i < chars.length; i++) {
            int idx = (chars.length - 1 - i) % 4;//段内位置  unit1
            int vidx = (chars.length - 1 - i) / 4;//段位置 unit2
            String s = digit[Integer.valueOf(String.valueOf(chars[i]))];
            if (!"零".equals(s)) {
                headMoney += s + unit1[idx] + unit2[vidx];
                zeroKeepFlag = false;
            } else if (i == chars.length - 1 || map.get("zero" + vidx) != null) {
                headMoney += "";
            } else {
                headMoney += s;
                zeroKeepFlag = true;
                map.put("zero" + vidx, true);//该段位已经出现0；
            }
            if (vidxtemp != vidx || i == chars.length - 1) {
                headMoney = headMoney.replaceAll(unit2[vidx], "");
                headMoney += unit2[vidx];
            }
            if (zeroKeepFlag && (chars.length - 1 - i) % 4 == 0) {
                headMoney = headMoney.replaceAll("零", "");
            }
        }
        return headMoney + endMoney;
    }

    /**
     * 生成指定范围的随机数
     * 
     * @param min
     * @param max
     * @return
     */
    public static long random(long min, long max) {
        // 生成金额Math.random() 0.0<=r<1
        return min + (long) (Math.random() * (max - min + 1));
    }

    public String format(BigDecimal amount, String unit) {
        return formatMoney(amount) + unit;
    }

    public String formatMoney(BigDecimal amount) {

        BigDecimal yw = new BigDecimal("10000");
        BigDecimal yy = yw.multiply(yw);
        if (amount.compareTo(yy) == 1) {
            BigDecimal[] awardAmount4Separator = amount.divideAndRemainder(yy);
            BigDecimal times = awardAmount4Separator[0].setScale(0,
                    RoundingMode.HALF_UP);// 整数位
            BigDecimal award = awardAmount4Separator[1];// 余数
            return times
                    + "<span>亿</span>"
                    + (award.compareTo(BigDecimal.ZERO) == 1 ? formatMoney(award)
                    : "");
        } else if (amount.compareTo(yw) == 1) {
            BigDecimal[] awardAmount4Separator = amount.divideAndRemainder(yw);
            BigDecimal times = awardAmount4Separator[0].setScale(0,
                    RoundingMode.HALF_UP);// 整数位
            BigDecimal award = awardAmount4Separator[1];// 余数
            return times
                    + "<span>万</span>"
                    + (award.compareTo(BigDecimal.ZERO) == 1 ? formatMoney(award)
                    : "");
        } else {
            return amount + "";
        }
    }
}
